package com.psfd.demo.controller;

import com.psfd.demo.entity.BookType;
import com.psfd.demo.service.IBookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 图书类型
 */
@RestController
public class BookTypeController {

    @Autowired
    private IBookTypeService bookTypeService;

    //增加
    @RequestMapping("/insert")
    public ModelAndView insert(BookType booktype){
        bookTypeService.save(booktype);
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.addObject("booktypeList",bookTypeService.list());
        modelAndView.setViewName("BookTypeQuery");
        return modelAndView;
    }

    //修改
    @RequestMapping("/update")
    public ModelAndView update(BookType booktype){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("booktype",booktype);
        modelAndView.setViewName("BookTypeUpdate");
        return modelAndView;
    }

    //修改
    @RequestMapping("/update1")
    public ModelAndView update1(BookType bookType){
        bookTypeService.updateById(bookType);
        return query();
    }

    //删除
    @RequestMapping("/delete")
    public ModelAndView deleted(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        bookTypeService.removeById(id);
        System.err.println("删除的ID为："+id);
        modelAndView.addObject("booktypeList",bookTypeService.list());
        modelAndView.setViewName("BookTypeQuery");
        return modelAndView;
    }

    //查询
    @RequestMapping("/query")
    public ModelAndView query(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("booktypeList",bookTypeService.list());
        System.err.println(bookTypeService.list());
        modelAndView.setViewName("BookTypeQuery");
        return modelAndView;
    }

}

